# -*- coding: utf-8 -*- 
#
# Description:
#
#  Definiton
#
#
#
# Changes:
#
# 2012-01-10
# Initial Commit
#
#
# Copyright 2010-2012, WyDev Team.
# Author: Polo35 (polo35580@hotmail.fr)
#
# Licenced under Academic Free License version 3.0
# Review WyGui README & LICENSE files for further details.

nname: 0
n 0(None)[self.device_id = wyspace_manager_dict[0]
self.total_blocks = wyspace_manager_dict[1]
self.free_blocks = wyspace_manager_dict[2]
self.used_blocks = wyspace_manager_dict[3]
self.size_of_block = wyspace_manager_dict[4]
]:
	i: 
	o: 

self.nodes: {0: <unpyclib.structure.node instance at 0xb76bf78c>}
nname: 6
n 6(None)[def __init__(self, wyspace_manager_dict):
	self.device_id = wyspace_manager_dict[0]
	self.total_blocks = wyspace_manager_dict[1]
	self.free_blocks = wyspace_manager_dict[2]
	self.used_blocks = wyspace_manager_dict[3]
	self.size_of_block = wyspace_manager_dict[4]

]:
	i: 
	o: 

self.nodes: {6: <unpyclib.structure.node instance at 0xb76b8e2c>}
nname: 0
n 0(None)[self.wdb = wdb
self.device = device
self.ref_wyscpacemanager = self.wdb.reference(config.space_manager_dbus_bus_name, config.space_manager_dbus_path_name)
self.max_timeshift_size = config.max_size_for_timeshift
self.total_rec = 0
self.total_used = 0
self.blind_free_space = 0
self.free_space = 0
self.live_manager = None
return None
]:
	i: 
	o: 

self.nodes: {0: <unpyclib.structure.node instance at 0xb76bfb8c>}
nname: 63
n 63(None)[return False
]:
	i: 3(), 33(), 61(AE)
	o: 

nname: 61
n 61(None)[]:
	i: 23(f)
	o: 63(AE)

nname: 33
n 33(None)[#, e
log.debug(str(e))
]:
	i: 23(t)
	o: 63()

nname: 23
n 23(<dummy_ex3> EXC_MATCH BaseException)[]:
	i: 0(except)
	o: 33(t), 61(f)

nname: 3
n 3(None)[return self._ReserveSpace(path, size)
]:
	i: 0(try)
	o: 63()

nname: 0
n 0(None)[]:
	i: 
	o: 3(try), 23(except)

nname: 23
n 23(None)[except BaseException, e:
	log.debug(str(e))
return False
]:
	i: 0()
	o: 

nname: 0
n 0(None)[try:
	return self._ReserveSpace(path, size)
]:
	i: 
	o: 23()

nname: 0
n 0(None)[try:
	return self._ReserveSpace(path, size)
except BaseException, e:
	log.debug(str(e))
return False
]:
	i: 
	o: 

self.nodes: {0: <unpyclib.structure.node instance at 0xb76bfb8c>}
nname: 63
n 63(None)[return False
]:
	i: 3(), 33(), 61(AE)
	o: 

nname: 61
n 61(None)[]:
	i: 23(f)
	o: 63(AE)

nname: 33
n 33(None)[#, e
log.debug(str(e))
]:
	i: 23(t)
	o: 63()

nname: 23
n 23(<dummy_ex3> EXC_MATCH BaseException)[]:
	i: 0(except)
	o: 33(t), 61(f)

nname: 3
n 3(None)[return self._ResizeSpace(path, size)
]:
	i: 0(try)
	o: 63()

nname: 0
n 0(None)[]:
	i: 
	o: 3(try), 23(except)

nname: 23
n 23(None)[except BaseException, e:
	log.debug(str(e))
return False
]:
	i: 0()
	o: 

nname: 0
n 0(None)[try:
	return self._ResizeSpace(path, size)
]:
	i: 
	o: 23()

nname: 0
n 0(None)[try:
	return self._ResizeSpace(path, size)
except BaseException, e:
	log.debug(str(e))
return False
]:
	i: 
	o: 

self.nodes: {0: <unpyclib.structure.node instance at 0xb76bfe8c>}
nname: 0
n 0(None)[stats = os.statvfs(config.base_timeshift)
return SpaceInfo([0, stats.f_blocks, stats.f_bfree, stats.f_blocks - stats.f_bfree, stats.f_bsize])
]:
	i: 
	o: 

self.nodes: {0: <unpyclib.structure.node instance at 0xb76bfeac>}
nname: 0
n 0(None)[self.update()
return self.free_space
]:
	i: 
	o: 

self.nodes: {0: <unpyclib.structure.node instance at 0xb76bf74c>}
nname: 0
n 0(None)[stats = self.SpaceInfo()
res = (stats.total_blocks * config.max_percentage_disk_use / 100 - stats.total_blocks - stats.free_blocks) * stats.size_of_block
return res
]:
	i: 
	o: 

self.nodes: {0: <unpyclib.structure.node instance at 0xb76bff6c>}
nname: 185
n 185(None)[total_for_timeshift = max_timeshift_size * config.live_count_max
self.max_timeshift_size = max_timeshift_size
self.total_rec = total_rec
self.total_used = total_used
self.blind_free_space = blind_free_space
self.free_space = self.blind_free_space + self.total_used - self.total_rec - total_for_timeshift
]:
	i: 142(), 171(), 184()
	o: 

nname: 184
n 184(None)[]:
	i: 155(f)
	o: 185()

nname: 171
n 171(None)[max_timeshift_size = config.min_size_for_timeshift
]:
	i: 155(t)
	o: 185()

nname: 155
n 155(max_timeshift_size <= config.min_size_for_timeshift)[]:
	i: 82(f)
	o: 171(t), 184(f)

nname: 142
n 142(None)[max_timeshift_size = config.max_size_for_timeshift
]:
	i: 82(t)
	o: 185()

nname: 82
n 82(max_timeshift_size >= config.max_size_for_timeshift)[blind_free_space = self._ExternalDiskSpace__freeSpace()
total_for_wyrecord = blind_free_space + total_used
total_for_timeshift = total_for_wyrecord - total_rec
max_timeshift_size = total_for_timeshift / config.live_count_max
]:
	i: 0(AL), 81()
	o: 142(t), 155(f)

nname: 81
n 81(None)[]:
	i: 15(AF), 31()
	o: 82()

nname: 31
n 31(None)[for live in self.live_manager.GetRunningLiveList():
rec = live.getRecordingSize()
total_rec += rec
used = live.getUsedSpace()
total_used += used
]:
	i: 15(for)
	o: 81()

nname: 15
n 15(None)[]:
	i: 0(loop)
	o: 31(for), 81(AF)

nname: 0
n 0(None)[total_rec = 0
total_used = 0
]:
	i: 
	o: 15(loop), 82(AL)

nname: 82
n 82(None)[blind_free_space = self._ExternalDiskSpace__freeSpace()
total_for_wyrecord = blind_free_space + total_used
total_for_timeshift = total_for_wyrecord - total_rec
max_timeshift_size = total_for_timeshift / config.live_count_max
if max_timeshift_size >= config.max_size_for_timeshift:
	max_timeshift_size = config.max_size_for_timeshift
else:
	if max_timeshift_size <= config.min_size_for_timeshift:
		max_timeshift_size = config.min_size_for_timeshift
total_for_timeshift = max_timeshift_size * config.live_count_max
self.max_timeshift_size = max_timeshift_size
self.total_rec = total_rec
self.total_used = total_used
self.blind_free_space = blind_free_space
self.free_space = self.blind_free_space + self.total_used - self.total_rec - total_for_timeshift
]:
	i: 31()
	o: 

nname: 31
n 31(None)[for live in self.live_manager.GetRunningLiveList():
	rec = live.getRecordingSize()
	total_rec += rec
	used = live.getUsedSpace()
	total_used += used
]:
	i: 0(for)
	o: 82()

nname: 0
n 0(None)[total_rec = 0
total_used = 0
]:
	i: 
	o: 31(for)

nname: 82
n 82(None)[blind_free_space = self._ExternalDiskSpace__freeSpace()
total_for_wyrecord = blind_free_space + total_used
total_for_timeshift = total_for_wyrecord - total_rec
max_timeshift_size = total_for_timeshift / config.live_count_max
if max_timeshift_size >= config.max_size_for_timeshift:
	max_timeshift_size = config.max_size_for_timeshift
else:
	if max_timeshift_size <= config.min_size_for_timeshift:
		max_timeshift_size = config.min_size_for_timeshift
total_for_timeshift = max_timeshift_size * config.live_count_max
self.max_timeshift_size = max_timeshift_size
self.total_rec = total_rec
self.total_used = total_used
self.blind_free_space = blind_free_space
self.free_space = self.blind_free_space + self.total_used - self.total_rec - total_for_timeshift
]:
	i: 0()
	o: 

nname: 0
n 0(None)[total_rec = 0
total_used = 0
for live in self.live_manager.GetRunningLiveList():
	rec = live.getRecordingSize()
	total_rec += rec
	used = live.getUsedSpace()
	total_used += used
]:
	i: 
	o: 82()

nname: 0
n 0(None)[total_rec = 0
total_used = 0
for live in self.live_manager.GetRunningLiveList():
	rec = live.getRecordingSize()
	total_rec += rec
	used = live.getUsedSpace()
	total_used += used
blind_free_space = self._ExternalDiskSpace__freeSpace()
total_for_wyrecord = blind_free_space + total_used
total_for_timeshift = total_for_wyrecord - total_rec
max_timeshift_size = total_for_timeshift / config.live_count_max
if max_timeshift_size >= config.max_size_for_timeshift:
	max_timeshift_size = config.max_size_for_timeshift
else:
	if max_timeshift_size <= config.min_size_for_timeshift:
		max_timeshift_size = config.min_size_for_timeshift
total_for_timeshift = max_timeshift_size * config.live_count_max
self.max_timeshift_size = max_timeshift_size
self.total_rec = total_rec
self.total_used = total_used
self.blind_free_space = blind_free_space
self.free_space = self.blind_free_space + self.total_used - self.total_rec - total_for_timeshift
]:
	i: 
	o: 

self.nodes: {0: <unpyclib.structure.node instance at 0xb76c592c>}
nname: 6
n 6(None)[def __init__(self, wdb, device):
	self.wdb = wdb
	self.device = device
	self.ref_wyscpacemanager = self.wdb.reference(config.space_manager_dbus_bus_name, config.space_manager_dbus_path_name)
	self.max_timeshift_size = config.max_size_for_timeshift
	self.total_rec = 0
	self.total_used = 0
	self.blind_free_space = 0
	self.free_space = 0
	self.live_manager = None
	return None

def ReserveSpace(self, path, size):
	try:
		return self._ReserveSpace(path, size)
	except BaseException, e:
		log.debug(str(e))
	return False

def ResizeSpace(self, path, size):
	try:
		return self._ResizeSpace(path, size)
	except BaseException, e:
		log.debug(str(e))
	return False

def SpaceInfo(self):
	stats = os.statvfs(config.base_timeshift)
	return SpaceInfo([0, stats.f_blocks, stats.f_bfree, stats.f_blocks - stats.f_bfree, stats.f_bsize])

def getAvailableDiskSpace(self):
	self.update()
	return self.free_space

def _ExternalDiskSpace__freeSpace(self):
	stats = self.SpaceInfo()
	res = (stats.total_blocks * config.max_percentage_disk_use / 100 - stats.total_blocks - stats.free_blocks) * stats.size_of_block
	return res

def update(self):
	total_rec = 0
	total_used = 0
	for live in self.live_manager.GetRunningLiveList():
		rec = live.getRecordingSize()
		total_rec += rec
		used = live.getUsedSpace()
		total_used += used
	blind_free_space = self._ExternalDiskSpace__freeSpace()
	total_for_wyrecord = blind_free_space + total_used
	total_for_timeshift = total_for_wyrecord - total_rec
	max_timeshift_size = total_for_timeshift / config.live_count_max
	if max_timeshift_size >= config.max_size_for_timeshift:
		max_timeshift_size = config.max_size_for_timeshift
	else:
		if max_timeshift_size <= config.min_size_for_timeshift:
			max_timeshift_size = config.min_size_for_timeshift
	total_for_timeshift = max_timeshift_size * config.live_count_max
	self.max_timeshift_size = max_timeshift_size
	self.total_rec = total_rec
	self.total_used = total_used
	self.blind_free_space = blind_free_space
	self.free_space = self.blind_free_space + self.total_used - self.total_rec - total_for_timeshift

]:
	i: 
	o: 

self.nodes: {6: <unpyclib.structure.node instance at 0xb76bf4ec>}
nname: 0
n 0(None)[from logger import log
from config import *
from peewee.analyse import Chrono
from peewee.analyse import timestep
import os
class SpaceInfo(object):
	def __init__(self, wyspace_manager_dict):
		self.device_id = wyspace_manager_dict[0]
		self.total_blocks = wyspace_manager_dict[1]
		self.free_blocks = wyspace_manager_dict[2]
		self.used_blocks = wyspace_manager_dict[3]
		self.size_of_block = wyspace_manager_dict[4]



class ExternalDiskSpace(object):
	def __init__(self, wdb, device):
		self.wdb = wdb
		self.device = device
		self.ref_wyscpacemanager = self.wdb.reference(config.space_manager_dbus_bus_name, config.space_manager_dbus_path_name)
		self.max_timeshift_size = config.max_size_for_timeshift
		self.total_rec = 0
		self.total_used = 0
		self.blind_free_space = 0
		self.free_space = 0
		self.live_manager = None
		return None

	def ReserveSpace(self, path, size):
		try:
			return self._ReserveSpace(path, size)
		except BaseException, e:
			log.debug(str(e))
		return False

	def ResizeSpace(self, path, size):
		try:
			return self._ResizeSpace(path, size)
		except BaseException, e:
			log.debug(str(e))
		return False

	def SpaceInfo(self):
		stats = os.statvfs(config.base_timeshift)
		return SpaceInfo([0, stats.f_blocks, stats.f_bfree, stats.f_blocks - stats.f_bfree, stats.f_bsize])

	def getAvailableDiskSpace(self):
		self.update()
		return self.free_space

	def _ExternalDiskSpace__freeSpace(self):
		stats = self.SpaceInfo()
		res = (stats.total_blocks * config.max_percentage_disk_use / 100 - stats.total_blocks - stats.free_blocks) * stats.size_of_block
		return res

	def update(self):
		total_rec = 0
		total_used = 0
		for live in self.live_manager.GetRunningLiveList():
			rec = live.getRecordingSize()
			total_rec += rec
			used = live.getUsedSpace()
			total_used += used
		blind_free_space = self._ExternalDiskSpace__freeSpace()
		total_for_wyrecord = blind_free_space + total_used
		total_for_timeshift = total_for_wyrecord - total_rec
		max_timeshift_size = total_for_timeshift / config.live_count_max
		if max_timeshift_size >= config.max_size_for_timeshift:
			max_timeshift_size = config.max_size_for_timeshift
		else:
			if max_timeshift_size <= config.min_size_for_timeshift:
				max_timeshift_size = config.min_size_for_timeshift
		total_for_timeshift = max_timeshift_size * config.live_count_max
		self.max_timeshift_size = max_timeshift_size
		self.total_rec = total_rec
		self.total_used = total_used
		self.blind_free_space = blind_free_space
		self.free_space = self.blind_free_space + self.total_used - self.total_rec - total_for_timeshift



]:
	i: 
	o: 

self.nodes: {0: <unpyclib.structure.node instance at 0xb76b886c>}
from logger import log
from config import *
from peewee.analyse import Chrono
from peewee.analyse import timestep
import os
class SpaceInfo(object):
	def __init__(self, wyspace_manager_dict):
		self.device_id = wyspace_manager_dict[0]
		self.total_blocks = wyspace_manager_dict[1]
		self.free_blocks = wyspace_manager_dict[2]
		self.used_blocks = wyspace_manager_dict[3]
		self.size_of_block = wyspace_manager_dict[4]



class ExternalDiskSpace(object):
	def __init__(self, wdb, device):
		self.wdb = wdb
		self.device = device
		self.ref_wyscpacemanager = self.wdb.reference(config.space_manager_dbus_bus_name, config.space_manager_dbus_path_name)
		self.max_timeshift_size = config.max_size_for_timeshift
		self.total_rec = 0
		self.total_used = 0
		self.blind_free_space = 0
		self.free_space = 0
		self.live_manager = None
		return None

	def ReserveSpace(self, path, size):
		try:
			return self._ReserveSpace(path, size)
		except BaseException, e:
			log.debug(str(e))
		return False

	def ResizeSpace(self, path, size):
		try:
			return self._ResizeSpace(path, size)
		except BaseException, e:
			log.debug(str(e))
		return False

	def SpaceInfo(self):
		stats = os.statvfs(config.base_timeshift)
		return SpaceInfo([0, stats.f_blocks, stats.f_bfree, stats.f_blocks - stats.f_bfree, stats.f_bsize])

	def getAvailableDiskSpace(self):
		self.update()
		return self.free_space

	def _ExternalDiskSpace__freeSpace(self):
		stats = self.SpaceInfo()
		res = (stats.total_blocks * config.max_percentage_disk_use / 100 - stats.total_blocks - stats.free_blocks) * stats.size_of_block
		return res

	def update(self):
		total_rec = 0
		total_used = 0
		for live in self.live_manager.GetRunningLiveList():
			rec = live.getRecordingSize()
			total_rec += rec
			used = live.getUsedSpace()
			total_used += used
		blind_free_space = self._ExternalDiskSpace__freeSpace()
		total_for_wyrecord = blind_free_space + total_used
		total_for_timeshift = total_for_wyrecord - total_rec
		max_timeshift_size = total_for_timeshift / config.live_count_max
		if max_timeshift_size >= config.max_size_for_timeshift:
			max_timeshift_size = config.max_size_for_timeshift
		else:
			if max_timeshift_size <= config.min_size_for_timeshift:
				max_timeshift_size = config.min_size_for_timeshift
		total_for_timeshift = max_timeshift_size * config.live_count_max
		self.max_timeshift_size = max_timeshift_size
		self.total_rec = total_rec
		self.total_used = total_used
		self.blind_free_space = blind_free_space
		self.free_space = self.blind_free_space + self.total_used - self.total_rec - total_for_timeshift



